//
//  SearchBarView.m
//  RuiHeApp
//
//  Created by Jun on 2023/4/26.
//

#import "SearchBarView.h"

@interface SearchBarView()<UITextFieldDelegate>



@property (nonatomic ,strong)  QMUIButton *searchBtn;

@end

@implementation SearchBarView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    [self setBackgroundColor:UIColor.clearColor];
    [self.layer setCornerRadius:35/2];
    
    _searchField = [[UITextField alloc] init];
    [_searchField setBackgroundColor:UIColor.clearColor];
    _searchField.autocorrectionType = UITextAutocorrectionTypeNo;
    [_searchField setHidden:YES];
    [self addSubview:_searchField];
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:CGRectMake(5, 5, 20, 20)];
    [btn setImage:[UIImage imageNamed:@"search_icon"] forState:UIControlStateNormal];
    [bgView addSubview:btn];
    [_searchField setLeftViewMode:UITextFieldViewModeAlways];
    [_searchField setClearButtonMode:UITextFieldViewModeWhileEditing];
    [_searchField setPlaceholder:@"请输入关键字".rh_localized];
    [_searchField setTextColor:FontColor(@"#333333")];
    [_searchField setDelegate:self];
    [_searchField setLeftView:bgView];
    
    _searchBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [_searchBtn setImage:[UIImage imageNamed:@"search_icon"] forState:UIControlStateNormal];
    [self addSubview:_searchBtn];
    
    
    kWeakSelf;
    [_searchField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(20);
        make.right.mas_offset(-15);
        make.centerY.mas_offset(0);
    }];
    
    [_searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(40);
        make.right.mas_offset(0);
        make.centerY.mas_offset(0);
    }];
    
    
    [[_searchBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [UIView animateWithDuration:0.5 animations:^{
            [weakSelf.searchBtn setHidden:YES];
            [weakSelf.searchField setHidden:NO];
            [weakSelf.searchField mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(kUI_WINDOW_W-60);
            }];
            [self setBackgroundColor:[UIColor whiteColor]];
        } completion:^(BOOL finished) {
            [weakSelf.searchField becomeFirstResponder];
        }];
    }];
    
    //searchBlock
    [self.searchField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if(weakSelf.searchBlock){
            weakSelf.searchBlock(x);
        }
    }];
}

- (void)reset{
    [_searchField resignFirstResponder];
    _searchField.text = @"";
    [UIView animateWithDuration:0.5 animations:^{
        [self.searchBtn setHidden:NO];
        [self.searchField setHidden:YES];
        [self->_searchField mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(20);
        }];
        [self setBackgroundColor:[UIColor clearColor]];
    } completion:^(BOOL finished) {
    
    }];
}
@end
